'use strict'
import React, { Component} from 'react';
import {
    View,
    StyleSheet,
    Platform
} from 'react-native';
import RootSiblings from 'react-native-root-siblings'
import HintContainer from './HintContainer';

let sibling = undefined
let touchStartEvent=function(event){
    event.preventDefault();
}

class Hint extends Component {

    static displayName = 'Hint';
    static propTypes = HintContainer.propTypes;

    static show = (options) => {
        if(Platform.OS=='web'){
            document.addEventListener('touchmove',touchStartEvent,{passive:false})
        };
        sibling = new RootSiblings(
            <HintContainer
                {...options}
                visible={true}
                destroy={()=>{Hint.hide()}}
            />
        );
        return sibling;
    };

    static hide = () => {
        if(Platform.OS=='web'){
            document.removeEventListener('touchmove',touchStartEvent,{passive:false})
        };
        if (sibling instanceof RootSiblings) {
             sibling.destroy();
        } else {
            console.warn(`Hint.hide expected a \`RootSiblings\` instance as argument.\nBut got \`${typeof sibling}\` instead.`);
        }
    };

    _hint = null;

    constructor(props) {
        super(props);
        this.state = {
            visible: true,
        };
    };

    _show = ()=>{
        this.container._open()
    }

    _close = ()=>{
        this.container._close()
    }

    componentWillMount = () => {
        this._hint = new RootSiblings(
            <HintContainer ref={(obj) =>{this.container = obj}}
                                 {...this.props}
            />
        );
    };

    componentWillReceiveProps = nextProps => {
        this._hint.update(
            <HintContainer ref={(obj) =>{this.container = obj}}
                                 {...nextProps}
            />
        );
    };



    componentWillUnmount = () => {
        this._hint.destroy();
    };

    render(){
        return null;
    }

}

export {
    RootSiblings as Manager
};

export default Hint;